package cn.lsh.comparator;

import cn.lsh.writable.Weather;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * 排序比较器，对key进行排序。这里设置年、月、温度三个维度，按年月正序，温度倒序
 *
 * @author liush
 */
public class WeatherSortComparator extends WritableComparator {

	public WeatherSortComparator() {
		//比较器必须重写构造方法，并调用父类的构造方法设置类型，并创建实例
		super(Weather.class, true);
	}

	@Override
	public int compare(WritableComparable a, WritableComparable b) {
		Weather wa = (Weather) a;
		Weather wb = (Weather) b;
		int i = Integer.compare(wa.getYear(), wb.getYear());
		if (i == 0) {
			i = Integer.compare(wa.getMonth(), wb.getMonth());
			if (i == 0) {
				return -Integer.compare(wa.getWd(), wb.getWd());
			}
		}
		return i;
	}
}
